package light.head.controller.oa;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import light.head.constant.Msg;
import light.head.constant.Parm;
import light.head.constant.Url;
import light.head.dao.oa.ConsumingDAO;
import light.head.dao.oa.ConsumingDetailDAO;
import light.head.dao.oa.ReturnDetailDAO;
import light.head.dao.oa.ReturnInfoDAO;
import light.head.dao.oa.ReturnItemDAO;
import light.head.dao.oa.StockDAO;
import light.head.dao.sys.OrganDAO;
import light.head.dao.sys.UserInfoDAO;
import light.head.enm.RESULT;
import light.head.framework.base.SupporterController;
import light.head.framework.entity.PageBean;
import light.head.framework.utils.MD5Utils;
import light.head.model.ConsumingDetail;
import light.head.model.ReturnDetail;
import light.head.model.ReturnInfo;
import light.head.model.ReturnItem;
import light.head.model.Stock;
import light.head.model.UserInfo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
/***
 * 物品归还记录以及归还
 * @author yy
 *
 */
@Controller
@RequestMapping(value = Url.C_RETURNRECORD)
public class ReturnRecordController extends SupporterController{
	
	private ReturnInfoDAO riDAO;
	private ReturnItemDAO itemDAO;
	private ReturnDetailDAO detailDAO;
	private OrganDAO organDAO;
	private ConsumingDAO conDAO;
	private ConsumingDetailDAO detDAO;
	private StockDAO sDAO;
	private UserInfoDAO uDAO;
	@RequestMapping(Url.INIT)
	public ModelAndView init(HttpServletRequest request, HttpServletResponse reponse) throws Exception {
		return new ModelAndView(Url.RETURN_INIT);
	}
	
	@RequestMapping(Url.QUERY)
	public ModelAndView query(HttpServletRequest request, HttpServletResponse reponse) throws Exception {
		PageBean pb = createPageBean(request);
		Map<String, String> map = createQueryMapByParmName(request, Parm.DELETED, Parm.CN,Parm.AFFIRM, Parm.TIME1, Parm.TIME2);
		Integer oc = organDAO.getRoot(cu_organCode(request)).getCode();
		if (RESULT.SUCCESS == riDAO.findByMap(organDAO.getSonCodeList(oc), pb, map)) {
			List<Object> conCodes = new ArrayList<Object>();
			for (Object o : pb.getResultList()) {
				ReturnInfo ret = (ReturnInfo) o;
				conCodes.add(ret.getCode());
			}
			// 将对应的明细放到对应的领用信息里
			List<ReturnItem> riList = itemDAO.findByConCodes(conCodes);
			if (null != riList && riList.size() > 0) {
				for (Object o : pb.getResultList()) {
					ReturnInfo ret = (ReturnInfo) o;
					ret.setItemList(new ArrayList<ReturnItem>());
					for (ReturnItem ri : riList) {
						if (ret.getCode().intValue() == ri.getRc().intValue()) {
							ret.getItemList().add(ri);
						}
					}
				}
			}
		}
		request.setAttribute(Parm.PAGE_BEAN, pb);
		return new ModelAndView(Url.RETURN_LIST);
	}
	
	
	//归还
	@RequestMapping(value = Url.EDIT)
	public ModelAndView edit(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String code = request.getParameter(Parm.CODE);
		if (isNotNull(code)) {
			ReturnInfo r = riDAO.get(Integer.parseInt(code));
			request.setAttribute(Parm.RETURNINFO, r);
		}
		return new ModelAndView(Url.RETURN_RETURN);
	}
	
	
	@RequestMapping(Url.UPDATESTATUS)
	public void updateStatus(String code, HttpServletRequest request, HttpServletResponse response) throws Exception {
		if (null != code) {
			String[] codes = code.split(",");
			for (String s : codes) {
				ReturnInfo ret = riDAO.get(Integer.parseInt(s));
				if (null != ret && (null == ret.getAffirm() || !ret.getAffirm())) {
					ret.setAffirm(true);
					if (RESULT.SUCCESS == riDAO.upd(ret)) {
					}
				}
			}
		}
	}
	
	
	@Transactional
	@RequestMapping(value = Url.RETURNSTOCK)
	public void returnStock(ReturnInfo ret, HttpServletRequest request, HttpServletResponse response) throws Exception{
		RESULT result = RESULT.ERROR;
		List<ReturnItem> list = ret.getItemList();			// 提交上来的数组
		ret.setCt(new Date());
		ret.setDeleted(false);
		ret.setAffirm(false);
		ret.setCu(cu_code(request));
		ret.setOc(cu_organCode(request));
		result = riDAO.add(ret);
		
		for (ReturnItem item : list) {
			//保存领用明细
			item.setRc(ret.getCode());
			itemDAO.add(item);
			//保存库存
			Integer oc = organDAO.getRoot(cu_organCode(request)).getCode();
			List<Stock> dbList = sDAO.findByGC(item.getGc(), organDAO.getSonCodeList(oc));
			if (null != dbList && dbList.size() > 0) {
				dbList.get(0).setStockcount(dbList.get(0).getStockcount() + item.getCount());
				result = sDAO.upd(dbList.get(0));
			}
			if (null != item.getDetailList() && item.getDetailList().size() > 0) {
				for (ReturnDetail d : item.getDetailList()) {
					List<ConsumingDetail> detailList = detDAO.findByTag(d.getTag());
					if (null != detailList && detailList.size() > 0) {
						for (ConsumingDetail detail : detailList) {
							detail.setDeleted(true);
						}
					d.setRic(item.getCode());
					result = detailDAO.add(d);
					}
				}
			}
		}
		if (result == RESULT.SUCCESS) {
			print(response, Msg.SUCCESS);
		} else {
			print(response, Msg.ERROR);
		}
	}
		
		
	/**
	 * 查询领用的物品
	 */
	@RequestMapping(Url.AID_INIT)
	public ModelAndView aidGoodsInit(HttpServletRequest request, HttpServletResponse response) throws Exception {
		request.setAttribute(Parm.INDEX, request.getParameter(Parm.INDEX));
		System.out.println(request.getParameter(Parm.INDEX));
		return new ModelAndView(Url.RETURN_AIT_INIT);
	}

	@RequestMapping(Url.AID_LIST)
	public ModelAndView aidGoodsList(HttpServletRequest request, HttpServletResponse response) throws Exception {
		PageBean pageBean = createPageBean(request);
		Map<String, String> map = createQueryMapByParmName(request, Parm.GN, Parm.OC, Parm.TIME1, Parm.TIME2);
		conDAO.findByMapAndCcAndSort(map, pageBean, cu_code(request));
			request.setAttribute(Parm.PAGE_BEAN, pageBean);
		return new ModelAndView(Url.RETURN_AIT_LIST);
	}

	
	@RequestMapping(Url.SHOW_TAG)
	public ModelAndView showTag(Integer code, HttpServletRequest request, HttpServletResponse response) throws Exception {
		if (code != null){
			ReturnInfo ret = riDAO.get(code);
			if (ret != null){
				//获得所有的明细
				List<ReturnItem> itemList = itemDAO.findByConCode(code);
				//获得所有的详情
				List<ReturnDetail> detailList = detailDAO.findByRCode(code);
				for (ReturnItem item : itemList){
					item.setDetailList(new ArrayList<ReturnDetail>());
					for (ReturnDetail detail : detailList){
						if (item.getCode().intValue() == detail.getRic().intValue()){
							item.getDetailList().add(detail);
						}
					}
				}
				ret.setItemList(itemList);
				request.setAttribute(Parm.INFO, ret);
			}
		}
		return new ModelAndView(Url.RETURN_SHOW_TAG);
	}
	
	@RequestMapping(value = "verify")
	public void tag(HttpServletRequest request, HttpServletResponse response){
		String tag = request.getParameter("tag");
		String gn = request.getParameter(Parm.GN);
		List<ConsumingDetail> detailList = detDAO.findByTagAndGn(tag,gn);
		if (detailList != null && detailList.size() > 0) {
			print(response, Msg.SUCCESS);
		} else {
			print(response, Msg.ERROR);
		}
	}
	/**
	 * 查询用户
	 */
	@RequestMapping(Url.AIDUSER_INIT)
	public ModelAndView aidUserInit(HttpServletRequest request, HttpServletResponse response) throws Exception {
		return new ModelAndView(Url.RETURN_USER_INIT);
	}

	@RequestMapping(Url.AIDUSER_LIST)
	public ModelAndView aidUserList(HttpServletRequest request, HttpServletResponse response) throws Exception {
		PageBean pageBean = createPageBean(request);
		Map<String, String> map = createQueryMapByParmName(request, Parm.REAL_NAME,Parm.DELETED);
		Integer oc = organDAO.getRoot(cu_organCode(request)).getCode();
		uDAO.findByMapAndOc(organDAO.getSonCodeList(oc),map,pageBean);
			request.setAttribute(Parm.PAGE_BEAN, pageBean);
		return new ModelAndView(Url.RETURN_USER_LIST);
	}
	
	//验证密码
	@RequestMapping(value = Url.AFFIRM)
	public void affirm(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String cc= request.getParameter(Parm.CC);
		String pwd = request.getParameter(Parm.PWD);

		if (cc == "" || pwd == "") {
			print(response,Msg.PARAMETER_ERROR);
			return;
		}
		UserInfo user = uDAO.getAffirm(Integer.parseInt(cc), pwd);
		if (user == null) {
			print(response, Msg.ERROR);
			return;
		}
		if (!user.getPwd().equals(MD5Utils.encryptTo32(pwd))) {
			print(response, Msg.EXISTS);
		} else {
			print(response, Msg.SUCCESS);
		}
	}

	@Autowired
	public void setRiDAO(ReturnInfoDAO riDAO) {
		this.riDAO = riDAO;
	}
	@Autowired
	public void setItemDAO(ReturnItemDAO itemDAO) {
		this.itemDAO = itemDAO;
	}
	@Autowired
	public void setOrganDAO(OrganDAO organDAO) {
		this.organDAO = organDAO;
	}
	@Autowired
	public void setDetailDAO(ReturnDetailDAO detailDAO) {
		this.detailDAO = detailDAO;
	}
	
	@Autowired
	public void setConDAO(ConsumingDAO conDAO) {
		this.conDAO = conDAO;
	}
	@Autowired
	public void setDetDAO(ConsumingDetailDAO detDAO) {
		this.detDAO = detDAO;
	}
	@Autowired
	public void setsDAO(StockDAO sDAO) {
		this.sDAO = sDAO;
	}
	@Autowired
	public void setuDAO(UserInfoDAO uDAO) {
		this.uDAO = uDAO;
	}
}
